package com.lz.ub.hm

import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import com.felhr.usbserial.UsbSerialDevice
import com.felhr.usbserial.UsbSerialInterface
import com.lz.ub.hm.impl.HMLinkManager
import com.lz.ub.hm.impl.HMUBReader
import com.lz.ub.hm.impl.UBCommHandler

class UBCommandManager private constructor() {

    companion object {
        //指令缓冲时间
        const val BUFFER_TIME = 1000L

        //配置相关
        private const val TIME_OUT = 3000
        private const val BAUD_RATE = 115200
        private const val DATA_BITS = 8
        private const val STOP_BITS = 1
        private const val PARITY = 0

        //instance
        private lateinit var INSTANCE: UBCommandManager
        private var hasInit = false

        //是否支持
        fun isSupport(device: UsbDevice): Boolean = UsbSerialDevice.isSupported(device)

        fun init() {
            if (!hasInit) INSTANCE = UBCommandManager()
            hasInit = true
        }

        fun getInstance(): UBCommandManager {
            if (!hasInit) throw IllegalStateException("You need init() first")
            return INSTANCE
        }
    }

    private class ReaderFactory : UBReader.UBReaderFactory {

        override fun create(callback: UBCommandCallback): UBReader =
            HMUBReader(UBCommHandler(callback))
    }

    private val operator: UBCommandOperator = UBCommandOperator(
        TIME_OUT, BAUD_RATE, DATA_BITS, STOP_BITS, PARITY,
        UsbSerialInterface.FLOW_CONTROL_OFF, ReaderFactory(), HMLinkManager()
    )

    fun isOpened(): Boolean = operator.isOpened()

    fun isAvailable(): Boolean = operator.isAvailable()

    fun hasLink(link: Int): Boolean = operator.hasLink(link)

    fun open(usbDevice: UsbDevice, conn: UsbDeviceConnection) {
        operator.open(usbDevice, conn)
    }

    fun close() {
        operator.close()
    }

    fun reset(resetCommand: String) {
        operator.reset(resetCommand)
    }

    fun resetAndClearDevices(resetCommand: String) {
        operator.resetAndClearDevices(resetCommand)
    }

    fun findDevice(link: Int): UBDevice? = operator.findDevice(link)

    fun addDevice(mac: String, property: Int, addType: Int, tag: Any? = null): UBDevice? =
        operator.addDevice(mac, property, addType, tag)

    // 注意删除此项前要确定此设备已经断开
    fun removeDevice(link: Int) {
        operator.removeDevice(link)
    }

    fun connectAll(callback: UBCallback? = null) {
        operator.connectAll(callback)
    }

    fun connect(link: Int, callback: UBCallback? = null) {
        operator.connect(link, callback)
    }

    fun disconnectAll(callback: UBCallback? = null) {
        operator.disconnectAll(callback)
    }

    fun disconnect(link: Int, callback: UBCallback? = null) {
        operator.disconnect(link, callback)
    }

    fun pushCommand(command: String) {
        operator.pushCommand(command)
    }

    fun pushCommand(link: Int, command: String) {
        operator.pushCommand(link, command)
    }

    fun addReadCallback(readCallback: UBReadCallback) {
        operator.addReadCallback(readCallback)
    }

    fun removeReadCallback(readCallback: UBReadCallback) {
        operator.removeReadCallback(readCallback)
    }

    fun addScanCallback(scanCallback: UBScanCallback) {
        operator.addScanCallback(scanCallback)
    }

    fun removeScanCallback(scanCallback: UBScanCallback) {
        operator.removeScanCallback(scanCallback)
    }
}